import { resolve } from "path";
import { defineConfig, UserConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import useRemoveConsole from "vite-plugin-remove-console";
// import { compression } from "vite-plugin-compression2";
import dayjs from "dayjs";
import allEnv from "./env.config";
import pkg from "./package.json";

export default defineConfig(env => {
    const isBuild = env.command === 'build';
    const mode = env.mode;
    const envConfig = allEnv[mode];
    const appInfo: AppInfo = {
        isBuild: isBuild,
        mode: mode,
        envConfig: envConfig,
        name: pkg.name || 'app',
        version: pkg.version || '1.0.0',
        buildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
    };
    const config: UserConfig = {
        resolve: {
            alias: {
                '@': resolve(__dirname, './src'),
            },
        },
        define: {
            __APP_INFO__: JSON.stringify(appInfo),
        },
        server: {
            host: envConfig.serverHost,
            port: envConfig.serverPort,
            open: false,
            cors: true,
            proxy: {
                '^/api/.*': {
                    target: envConfig.apiTarget,
                    changeOrigin: false,
                },
            },
        },
        preview: {
            port: envConfig.serverPort,
        },
        plugins: [
            uni(),
        ],
    };
    if (isBuild) {
        config.plugins?.push(useRemoveConsole());
        // config.plugins?.push(compression({
        //     algorithm: "gzip",
        //     skipIfLargerOrEqual: false,
        // }));
    }
    return config;
});
